<%--
  Created by IntelliJ IDEA.
  User: LXJ
  Date: 2021/6/17
  Time: 14:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/component/pear/css/pear.css"/>
</head>
<%--添加新的模板--%>
<script type="text/html" id="addTpl">
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" lay-verify="title" autocomplete="off"
                                   placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="phone" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="0" title="男">
                            <input type="radio" name="sex" value="1" title="女" checked>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="address" required lay-verify="required" placeholder="请输入地址"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">证件号码</label>
                        <div class="layui-input-block">
                            <input type="text" name="idCard" required lay-verify="required" placeholder="请输入证件号码"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="custom-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</script>
<%--添加的修改的模板--%>
<script type="text/html" id="updateTpl">
    <form class="layui-form" action="" lay-filter="update-custom">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-block">
                            <input name="id" hidden>
                            <input type="text" name="name" lay-verify="title" autocomplete="off"
                                   placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="phone" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="0" title="男">
                            <input type="radio" name="sex" value="1" title="女" checked>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="address" required lay-verify="required" placeholder="请输入地址"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">证件号码</label>
                        <div class="layui-input-block">
                            <input type="text" name="idCard" required lay-verify="required" placeholder="请输入证件号码"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="custom-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</script>
<%--添加分配角色的模板--%>
<script type="text/html" id="assignTpl">
    <form class="layui-form" action="" lay-filter="assign-custom">
        <div class="mainBox">
            <input name="id" type="text" hidden>
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色</label>
                        <div class="layui-input-block">
                            <ul id="roleTree" class="dtree"></ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="custom-assign">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</script>

<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body ">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <form class="layui-form" action="" onsubmit="return false">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" required placeholder="请输入输入框内容" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">证件号码</label>
                        <div class="layui-input-inline">
                            <input type="text" name="idCard" required placeholder="请输入输入框内容" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-inline">
                            <select name="sex" lay-search>
                                <option value=""></option>
                                <option value="0">男</option>
                                <option value="1">女</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-inline">
                            <input type="text" name="address" required placeholder="请输入输入框内容" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <hr/>
                    <div class="layui-form-item layui-input-block" align="center">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="custom-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </form>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="custom-table" lay-filter="custom-table"></table>
    </div>
</div>

<script type="text/html" id="custom-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="custom-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="custom-enable">
    <%--    custom-id 代表自定义的属性 checked=true 和false 每页效果--%>
    <input type="checkbox" custom-id="{{d.id}}" name="enable" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="custom-enable"
           {{d.deleted==1 ? 'checked': ''}}></input>
</script>

<script type="text/html" id="custom-sex">
    {{#if (d.sex == 0) { }}
    <span>男</span>
    {{# }else if(d.sex == 1){ }}
    <span>女</span>
    {{# } }}
</script>

<script type="text/html" id="custom-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
</script>

<script src="${pageContext.request.contextPath}/resources/component/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/resources/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dtree'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dtree = layui.dtree;


        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '姓名',
                    field: 'name',
                    align: 'center',
                    width: 100
                },
                {
                    title: '性别',
                    field: 'sex',
                    align: 'center',
                    width: 80,
                    templet: '#custom-sex'
                },
                {
                    title: '电话',
                    field: 'phone',
                    align: 'center'
                },
                {
                    title: '地址',
                    field: 'address',
                    align: 'center'
                },
                {
                    title: '证件号码',
                    field: 'idCard',
                    align: 'center'
                },
                {
                    title: '注册',
                    field: 'createTime',
                    align: 'center',
                    templet: '#custom-createTime'
                },
                {
                    title: '操作',
                    toolbar: '#custom-bar',
                    align: 'center',
                    width: 150
                }
            ]
        ]

        //Table 数据的加载
        table.render({
            elem: '#custom-table',
            url: '${pageContext.request.contextPath}/buscustom/findByPage.do',
            page: true,
            // 对象的属性和table的列对应
            cols: cols,
            skin: 'line',
            toolbar: '#custom-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            // res == Result
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.count, //解析数据长度
                    "data": res.data.data //解析数据列表
                }
            },
            response: {
                statusCode: 200
            }
        });

        table.on('tool(custom-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'assign') {
                window.assign(obj);
            }
        });

        table.on('toolbar(custom-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(custom-query)', function (data) {
            table.reload('custom-table', {
                where: data.field
            })
            return false;
        });


        window.add = function () {
            layer.open({
                // 页面层
                type: 1,
                title: '新增',
                shade: 0.1,
                area: [common.isModile() ? '100%' : '600px', common.isModile() ? '100%' : '500px'],
                content: $("#addTpl").html(),
                success: function (layerno, index) {
                    // 渲染form
                    form.render()
                    // 给按钮绑定事件
                    form.on('submit(custom-save)', function (data) {
                        $.ajax({
                            url: '${pageContext.request.contextPath}/buscustom/add.do',
                            data: JSON.stringify(data.field),
                            dataType: 'json',
                            contentType: 'application/json',
                            type: 'post',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                        // 关闭当前的弹层
                                        layer.close(index);
                                        // 重新加载我们的表格
                                        table.reload("custom-table");
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 2, time: 1000});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 1,
                title: '修改',
                shade: 0.1,
                area: ['600px', '500px'],
                content: $("#updateTpl").html(),
                success: function (layero, index) {
                    // 数据的回显update-custom
                    form.val("update-custom", obj.data);
                    form.render();
                    form.on('submit(custom-save)', function (data) {
                        $.ajax({
                            url: '${pageContext.request.contextPath}/buscustom/update.do',
                            data: JSON.stringify(data.field),
                            dataType: 'json',
                            contentType: 'application/json',
                            type: 'post',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                        layer.close(index);//关闭当前页
                                        table.reload("custom-table");
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 2, time: 1000});
                                }
                            }
                        })
                        return false;
                    });

                }
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该用户', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "${pageContext.request.contextPath}/buscustom/delete.do/" + obj.data['id'],
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {

            // 获取到你选择的数据 ,checkField 将会变成一个使用, 分割的字符串
            //id: 用户id
            var checkIds = common.checkField(obj, 'id');

            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除这些用户', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "${pageContext.request.contextPath}/buscustom/delete.do/" + checkIds,
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('custom-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('custom-table');
        }
    })
</script>
</body>
</html>

